<?php
class ControllerDeployConfirm extends Controller {
    public function index(){

        $this->load->model('checkout/order');
        $this->language->load('checkout/checkout');
        $this->load->model('tool/image');
        $this->data['column_name'] = $this->language->get('column_name');
        $this->data['column_model'] = $this->language->get('column_model');
        $this->data['column_quantity'] = $this->language->get('column_quantity');
        $this->data['column_price'] = $this->language->get('column_price');
        $this->data['column_total'] = $this->language->get('column_total');

        $this->data['text_recurring_item'] = $this->language->get('text_recurring_item');
        $this->data['text_payment_profile'] = $this->language->get('text_payment_profile');

        $this->data['payment'] = $this->getChild('payment/cod');
        $this->data['products'] = array();

        $this->data['vpc_price']=array();
        $this->data['eips']=array();
        $total_price=0;



        $data = array();

        foreach ($this->cart->getProducts() as $product) {
            $option_data = array();

            if ($product['image']) {
                $image = $this->model_tool_image->resize($product['image'], $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height'));
            } else {
                $image = '';
            }

            foreach ($product['option'] as $option) {
                if ($option['type'] != 'file') {
                    $value = $option['option_value'];
                } else {
                    $filename = $this->encryption->decrypt($option['option_value']);

                    $value = utf8_substr($filename, 0, utf8_strrpos($filename, '.'));
                }

                $option_data[] = array(
                    'name'  => $option['name'],
                    'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value)
                );
            }


            $profile_description = '';

            if ($product['recurring']) {
                $frequencies = array(
                    'day' => $this->language->get('text_day'),
                    'week' => $this->language->get('text_week'),
                    'semi_month' => $this->language->get('text_semi_month'),
                    'month' => $this->language->get('text_month'),
                    'year' => $this->language->get('text_year'),
                );

                if ($product['recurring_trial']) {
                    $recurring_price = $this->currency->format($this->tax->calculate($product['recurring_trial_price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')));
                    $profile_description = sprintf($this->language->get('text_trial_description'), $recurring_price, $product['recurring_trial_cycle'], $frequencies[$product['recurring_trial_frequency']], $product['recurring_trial_duration']) . ' ';
                }

                $recurring_price = $this->currency->format($this->tax->calculate($product['recurring_price'] * $product['quantity'], $product['tax_class_id'], $this->config->get('config_tax')));

                if ($product['recurring_duration']) {
                    $profile_description .= sprintf($this->language->get('text_payment_description'), $recurring_price, $product['recurring_cycle'], $frequencies[$product['recurring_frequency']], $product['recurring_duration']);
                } else {
                    $profile_description .= sprintf($this->language->get('text_payment_until_canceled_description'), $recurring_price, $product['recurring_cycle'], $frequencies[$product['recurring_frequency']], $product['recurring_duration']);
                }
            }


            //get instance price


            $paramter=(array)$this->session->data['app']['deploy_config'][$product['product_id']]['params']['parameter'];
            $host_type=$paramter['instance_type'];
            $os_type=$paramter['os_type'];
            $region=$this->session->data['app']['deploy_config'][$product['product_id']]['params'][
            'region'];

            $host_data=array(
                'cloud_type' => 'qingyun',
                'region' => $region,
                'host_type' => $host_type,
                'os_type' => $os_type,
            );

            $host_price=$this->price->getPrice($host_data);


            $total_price=$total_price+$host_price;



            $data['product_deploy'][]=array(
                'order_product_id'          => $product['product_id'],
                'app_name'                => $this->session->data['app']['deploy_config'][$product['product_id']]['params']['name'],
                'vpc_name'                => $this->session->data['app']['deploy_config'][$product['product_id']]['params']['network']['vpc_mode']['vpc_name'],
                'subnet_name'                => $this->session->data['app']['deploy_config'][$product['product_id']]['params']['network']['vpc_mode']['vxnet_name'],
                'instance_type'                => $this->session->data['app']['deploy_config'][$product['product_id']]['params']['parameter']->instance_type,
                'super_password'                => $this->session->data['app']['basic']['super_password'],
                'price'                =>$host_price
            );

            $this->data['products'][] = array(
                'key'                 => $product['key'],
                'product_id'          => $product['product_id'],
                'name'                => $product['name'],
                'app_name'                => $this->session->data['app']['deploy_config'][$product['product_id']]['params']['name'],
                'instance_type'                => $this->session->data['app']['deploy_config'][$product['product_id']]['params']['parameter']->instance_type,
                'model'               => $product['model'],
                'image'               => $image,
                'option'              => $option_data,
                'quantity'            => $product['quantity'],
                'subtract'            => $product['subtract'],
                'hour_price'               => $this->currency->format($this->tax->calculate($host_price, $product['tax_class_id'], $this->config->get('config_tax'))),
                'month_price'               => $this->currency->format($this->tax->calculate($host_price*720, $this->config->get('config_tax_id'), $this->config->get('config_tax')),2),
                'href'                => $this->url->link('product/product', 'product_id=' . $product['product_id']),
                'recurring'           => $product['recurring'],
                'profile_name'        => $product['profile_name'],
                'profile_description' => $profile_description,
            );
        }



        if(isset($this->session->data['app']['eip_deploy'])){
            foreach($this->session->data['app']['eip_deploy'] as $eip){
                //get eip price
                $eip_data=array(
                    'cloud_type' => 'qingyun',
                    'region' => $eip['params']['region'],
                    'bandwidth' => $eip['params']['bandwidth'],
                );

                $eip_price=$this->price->getPrice($eip_data);
                $total_price=$total_price+$eip_price;
                $this->data['eips'][]=array(
                    'eip_image'                => $this->model_tool_image->resize('data/eip_icon.jpg', $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height')),
                    'eip_name'                => $eip['params']['name'],
                    'bandwidth'                =>$eip['params']['bandwidth'],
                    'hour_price'               => $this->currency->format($this->tax->calculate($eip_price, $product['tax_class_id'], $this->config->get('config_tax'))),
                    'month_price'               => $this->currency->format($this->tax->calculate($eip_price*720, $this->config->get('config_tax_id'), $this->config->get('config_tax')),2)
                );

                $data['eips_info'][]=array(
                    'eip_name'                => $eip['params']['name'],
                    'bandwidth'                =>$eip['params']['bandwidth'],
                    'price'               => $eip_price
                );


            }
        }
        if(isset($this->session->data['app']['vpc_deploy'])){
            foreach($this->session->data['app']['vpc_deploy'] as $vpc){
                //get vpc _price

                //get eip price
                $vpc_data=array(
                    'cloud_type' => 'qingyun',
                    'region' => $vpc['params']['region'],
                    'vpc_type' => 'small',
                );
                $vpc_price=$this->price->getPrice($vpc_data);
                $total_price=$total_price+$vpc_price;
                $this->data['vpc_price'][]=array(
                    'vpc_image'                => $this->model_tool_image->resize('data/vpc_icon.jpg', $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height')),
                    'vpc_name'                => $vpc['params']['name'],
                    'vpc_type'                => 'Small',
                    'hour_price'               => $this->currency->format($this->tax->calculate($vpc_price, $product['tax_class_id'], $this->config->get('config_tax'))),
                    'month_price'               => $this->currency->format($this->tax->calculate($vpc_price*720, $this->config->get('config_tax_id'), $this->config->get('config_tax')),2),
                );
                $data['vpc_price_info'][]=array(
                    'vpc_name'                => $vpc['params']['name'],
                    'vpc_type'                =>'标准',
                    'price'               => $vpc_price
                );
            }
        }
        //get harddisk related
         if(isset($this->session->data['app']['hd_deploy'])){
         	 foreach($this->session->data['app']['hd_deploy'] as $hd_deploy){

         	 	if($hd_deploy['cmd'] == "post" ){
                    $hd_data=array(
                        'cloud_type' => 'qingyun',
                        'region' => $hd_deploy['params']['region'],
                        'hd_type' => $hd_deploy['params']['type'],
                        'hd_size' => $hd_deploy['params']['size'],
                    );

	                $hd_price = $this->price->getprice($hd_data);
	                $total_price=$total_price+$hd_price['price_hour'];
	                $this->data['hd_price'][]=array(
	                    'hd_image'                => $this->model_tool_image->resize('data/vpc_icon.jpg', $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height')),
	                    'hd_name'                => $hd_deploy['hd_name'],
	                    'type'                => $hd_deploy['params']['type'],
	                    'size'                => $hd_deploy['params']['size'],

                        'hour_price'               => $this->currency->format($this->tax->calculate($vpc_price, $product['tax_class_id'], $this->config->get('config_tax'))),
                        'month_price'               => $this->currency->format($this->tax->calculate($vpc_price*720, $this->config->get('config_tax_id'), $this->config->get('config_tax')),2),
	                );
	                $data['hd_price_info'][]=array(
	                    'hd_name'                => $hd_deploy['hd_name'],
	                    'type'                => $hd_deploy['params']['type'],
	                    'size'                => $hd_deploy['params']['size'],
	                    'price'               => $hd_price['price_hour']
	                );
         	 	
         	 		
         	 	}
         	 	
         	 	
          }
           
        }
        
        //end of harddisk
        
        


        $this->data['month_total'] =$this->currency->format($this->tax->calculate($total_price*720, $this->config->get('config_tax_id'), $this->config->get('config_tax')),2);
        $this->data['hour_total'] = $this->currency->format($this->tax->calculate($total_price, 0 , $this->config->get('config_tax')));

        $data['invoice_prefix'] = $this->config->get('config_invoice_prefix');
        $data['store_id'] = $this->config->get('config_store_id');
        $data['store_name'] = $this->config->get('config_name');

        if ($data['store_id']) {
            $data['store_url'] = $this->config->get('config_url');
        } else {
            $data['store_url'] = HTTP_SERVER;
        }

        if ($this->customer->isLogged()) {
            $data['customer_id'] = $this->customer->getId();
            $data['customer_group_id'] = $this->customer->getCustomerGroupId();
            $data['firstname'] = $this->customer->getFirstName();
            $data['lastname'] = $this->customer->getLastName();
            $data['email'] = $this->customer->getEmail();
            $data['telephone'] = $this->customer->getTelephone();
            $data['fax'] = $this->customer->getFax();

            $this->load->model('account/address');

        } elseif (isset($this->session->data['guest'])) {
            $data['customer_id'] = 0;
            $data['customer_group_id'] = $this->session->data['guest']['customer_group_id'];
            $data['firstname'] = $this->session->data['guest']['firstname'];
            $data['lastname'] = $this->session->data['guest']['lastname'];
            $data['email'] = $this->session->data['guest']['email'];
            $data['telephone'] = $this->session->data['guest']['telephone'];
            $data['fax'] = $this->session->data['guest']['fax'];

        }

        $data['payment_firstname'] = "";
        $data['payment_lastname'] = "";
        $data['payment_company'] = "";
        $data['payment_company_id'] = "";
        $data['payment_tax_id'] = "";
        $data['payment_address_1'] = "";
        $data['payment_address_2'] = "";
        $data['payment_city'] = "";
        $data['payment_postcode'] = "";
        $data['payment_zone'] = "";
        $data['payment_zone_id'] = "";
        $data['payment_country'] = "";
        $data['payment_country_id'] = "";
        $data['payment_address_format'] = "";

        $data['payment_method'] = '';

        $data['payment_code'] = '';


        $data['shipping_firstname'] = '';
        $data['shipping_lastname'] = '';
        $data['shipping_company'] = '';
        $data['shipping_address_1'] = '';
        $data['shipping_address_2'] = '';
        $data['shipping_city'] = '';
        $data['shipping_postcode'] = '';
        $data['shipping_zone'] = '';
        $data['shipping_zone_id'] = '';
        $data['shipping_country'] = '';
        $data['shipping_country_id'] = '';
        $data['shipping_address_format'] = '';
        $data['shipping_method'] = '';
        $data['shipping_code'] = '';






        $product_data = array();

        foreach ($this->cart->getProducts() as $product) {
            $option_data = array();

            if ($product['image']) {
                $image = $this->model_tool_image->resize($product['image'], $this->config->get('config_image_cart_width'), $this->config->get('config_image_cart_height'));
            } else {
                $image = '';
            }


            foreach ($product['option'] as $option) {
                if ($option['type'] != 'file') {
                    $value = $option['option_value'];
                } else {
                    $value = $this->encryption->decrypt($option['option_value']);
                }

                $option_data[] = array(
                    'product_option_id'       => $option['product_option_id'],
                    'product_option_value_id' => $option['product_option_value_id'],
                    'option_id'               => $option['option_id'],
                    'option_value_id'         => $option['option_value_id'],
                    'name'                    => $option['name'],
                    'value'                   => $value,
                    'type'                    => $option['type']
                );
            }

            $product_data[] = array(
                'product_id' => $product['product_id'],
                'name'       => $product['name'],
                'model'      => $product['model'],
                'image'      => $image,
                'option'     => $option_data,
                'download'   => $product['download'],
                'quantity'   => $product['quantity'],
                'subtract'   => $product['subtract'],
                'price'      => $product['price'],
                'total'      => $product['total'],
                'tax'        => $this->tax->getTax($product['price'], $product['tax_class_id']),
                'reward'     => $product['reward']
            );
        }

        // Gift Voucher
        $voucher_data = array();

        if (!empty($this->session->data['vouchers'])) {
            foreach ($this->session->data['vouchers'] as $voucher) {
                $voucher_data[] = array(
                    'description'      => $voucher['description'],
                    'code'             => substr(md5(mt_rand()), 0, 10),
                    'to_name'          => $voucher['to_name'],
                    'to_email'         => $voucher['to_email'],
                    'from_name'        => $voucher['from_name'],
                    'from_email'       => $voucher['from_email'],
                    'voucher_theme_id' => $voucher['voucher_theme_id'],
                    'message'          => $voucher['message'],
                    'amount'           => $voucher['amount']
                );
            }
        }

        $data['products'] = $product_data;
        $data['vouchers'] = $voucher_data;
        $data['totals'] = "";
        $data['total'] = $total_price;
        $data['comment'] = "";

        if (isset($this->request->cookie['tracking'])) {
            $this->load->model('affiliate/affiliate');

            $affiliate_info = $this->model_affiliate_affiliate->getAffiliateByCode($this->request->cookie['tracking']);
            $subtotal = $this->cart->getSubTotal();

            if ($affiliate_info) {
                $data['affiliate_id'] = $affiliate_info['affiliate_id'];
                $data['commission'] = ($subtotal / 100) * $affiliate_info['commission'];
            } else {
                $data['affiliate_id'] = 0;
                $data['commission'] = 0;
            }
        } else {
            $data['affiliate_id'] = 0;
            $data['commission'] = 0;
        }

        $data['language_id'] = $this->config->get('config_language_id');
        $data['currency_id'] = $this->currency->getId();
        $data['currency_code'] = $this->currency->getCode();
        $data['currency_value'] = $this->currency->getValue($this->currency->getCode());
        $data['ip'] = $this->request->server['REMOTE_ADDR'];

        if (!empty($this->request->server['HTTP_X_FORWARDED_FOR'])) {
            $data['forwarded_ip'] = $this->request->server['HTTP_X_FORWARDED_FOR'];
        } elseif(!empty($this->request->server['HTTP_CLIENT_IP'])) {
            $data['forwarded_ip'] = $this->request->server['HTTP_CLIENT_IP'];
        } else {
            $data['forwarded_ip'] = '';
        }

        if (isset($this->request->server['HTTP_USER_AGENT'])) {
            $data['user_agent'] = $this->request->server['HTTP_USER_AGENT'];
        } else {
            $data['user_agent'] = '';
        }

        if (isset($this->request->server['HTTP_ACCEPT_LANGUAGE'])) {
            $data['accept_language'] = $this->request->server['HTTP_ACCEPT_LANGUAGE'];
        } else {
            $data['accept_language'] = '';
        }




        $this->session->data['order_id'] = $this->model_checkout_order->addOrder($data);






        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/deploy/deploy_confirm.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/deploy/deploy_confirm.tpl';
        } else {
            $this->template = 'default/template/deploy/deploy_confirm.tpl';
        }

        $this->response->setOutput($this->render());

    }
    public function validate(){
        $this->language->load('deploy/confirm');

        $json = array();
        // Validate if customer is logged in.
        if (!$this->customer->isLogged()) {
            $json['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');
        }

        if (!$json) {
            // Validate APP Name
//            foreach($this->request->post['lock_for_app'] as $aid => $lock_for_app){
//                if (!$lock_for_app) {
//                    $json['error']['lock_for_app'][$aid] = $this->language->get('error_lock_for_app');
//                }
//            }

			
			 // Validate hd
//            foreach($this->request->post['hd'] as $aid => $hd){
//                if ($hd=="hd_default" || $hd=="hd_creat") {
//                    $json['error']['hd'][$aid] = $this->language->get('error_hd');
//                }
//                
//            }
            
            // Validate vpc
            foreach($this->request->post['vpc'] as $aid => $vpc){
                if ($vpc=="vpc_default") {
                    $json['error']['vpc'][$aid] = $this->language->get('error_vpc');
                }
                if(!isset($this->session->data['app']['deploy_app_info'][$aid])){
                    $json['error']['lock_for_app'][$aid] = $this->language->get('error_lock_for_app');
                }else{
                    $this->session->data['app']['deploy_config'][$aid]['params']['network']['vpc_mode']['vpc_name']=$vpc;
                }
            }
            // Validate subnet
           
            
            foreach($this->request->post['subnet'] as $aid => $subnet){

                if(!isset($this->session->data['app']['deploy_config'][$aid]['params']['network']['vpc_mode']['vxnet_name'])){
                    $json['error']['lock_for_app'][$aid] = $this->language->get('error_lock_for_app');
                }else{
                    $this->session->data['app']['deploy_config'][$aid]['params']['network']['vpc_mode']['vxnet_name']=$subnet;
                }
                if (empty($subnet)||$subnet=='subnet_default'||$subnet=='subnet_edit') {
                    $json['error']['subnet'][$aid] = $this->language->get('error_subnet');
                }
            }
            //Validate if the ssh port is duplicate in the same vpc
            unset($this->session->data['ssh_port']);
            unset($this->session->data['web_port']);
            foreach($this->request->post['vpc'] as $aid => $vpc){
            	
            	if(isset($this->session->data['app']['deploy_config'][$aid]['params']['parameter']->access_app_control['ssh_control']['port_mapping'])){
            		 
            		$ssh_port = $this->session->data['app']['deploy_config'][$aid]['params']['parameter']->access_app_control['ssh_control']['port_mapping'];
            		$region = $this->session->data['app']['deploy_config'][$aid]['params']['region'];
	               
	               	if(!isset($this->session->data['ssh_port'][$region][$vpc][$ssh_port]) && !$this->backend->check_ssh_port_exist($region,$vpc, $this->customer->getEmail(), $this->customer->getPassPlain(),$ssh_port)){
	                	
	                	$this->session->data['ssh_port'][$region][$vpc][$ssh_port] = $ssh_port;
	                }
	                else{
	                	
	                	 $json['error']['ssh_port_error'][$aid] = $this->language->get('error_ssh_port_exist');
	                	 
	                	 
	                	 
	                }
            	}
            	//echo $this->session->data['app']['deploy_config'][$aid]['params']['parameter']->access_app_control['web_control']['port_mapping'];
            	//web port validation newly add 01.28
            	if(isset($this->session->data['app']['deploy_config'][$aid]['params']['parameter']->access_app_control['web_control']['port_mapping'])){
            		 
            		$web_port = $this->session->data['app']['deploy_config'][$aid]['params']['parameter']->access_app_control['web_control']['port_mapping'];
            		$region = $this->session->data['app']['deploy_config'][$aid]['params']['region'];
	               
	               	if(!isset($this->session->data['web_port'][$region][$vpc][$web_port]) && !$this->backend->check_ssh_port_exist($region,$vpc, $this->customer->getEmail(), $this->customer->getPassPlain(),$web_port)){
	                	
	                	$this->session->data['web_port'][$region][$vpc][$web_port] = $web_port;
	                }
	                else{
	                	
	                	 $json['error']['web_port_error'][$aid] = $this->language->get('error_web_port_exist');
	                	 
	                	 
	                	 
	                }
            	}
            	
                
                
            }
            //end of Validate if the ssh port is duplicate in the same vpc
            

        }


		
        // Update session
       
        unset($this->session->data['app']['appconfig']);
        if (!$json) {
        
        	//update harddisk session
        	
        	  foreach($this->request->post['hd'] as $key =>$hd){
        	  	 if ($hd !="hd_default" && $hd !="hd_creat") {
        	  	 	
        	  	 	$this->session->data['app']['appconfig'][$key]['hd']=$hd;
        	  	 }
                
            }
         
        	//end
        	
        	

            foreach($this->request->post['vpc'] as $key =>$vpc){
                $this->session->data['app']['appconfig'][$key]['vpc']=$vpc;
            }
            foreach($this->request->post['subnet'] as $key =>$subnet){
                $this->session->data['app']['appconfig'][$key]['subnet']=$subnet;
            }

           //format hd 
           $hd_config_info = array();
           $region_hd = array();
            //格式化循环APP所用到的VPC Name 和subnetname
            $appconfig_info=array();
            foreach($this->session->data['app']['appconfig'] as $app_id =>$appconfig){
                if(!isset($appconfig_info[$appconfig['vpc']])||!in_array($appconfig['subnet'] ,$appconfig_info[$appconfig['vpc']])){
                    $appconfig_info[$appconfig['vpc']][]=$appconfig['subnet'];
                }
                
//                if(!isset($hd_config_info[$app_id])){
//                	 
//                	
//                	 $hd_config_info[$app_id]= $appconfig['hd'];
//                }
//                
            }
          	 foreach($this->request->post['region'] as $key => $region){
            	foreach($this->request->post['hd'] as $app_id => $hd){
            	 if ($hd !="hd_default" && $hd !="hd_creat") {
            	 	if($key == $app_id){
            			 $hd_config_info[$app_id] = array('region'=>$region,'name'=>$hd,'app_id'=>$app_id);
            			 $region_hd[$region][$hd] = array('region'=>$region,'name'=>$hd,'app_id'=>$app_id);
            		}
            	 }
            		
            	}
            }
          	
            //fixed 

            unset( $this->session->data['app']['vpc_deploy']);
            unset( $this->session->data['app']['eip_deploy']);
            unset( $this->session->data['app']['hd_deploy']);
			if(count($hd_config_info) > 0){ 
            foreach($hd_config_info as $app_id => $hd_name){
            	 if(isset($this->session->data['app']['hd'])){
                    foreach($this->session->data['app']['hd']  as $session_app_id => $hd_session_info){
                    	 //如果hd是新建的用post
                    	$hd_info = $this->backend->get_hd_info($hd_session_info['region'],$hd_name, $this->customer->getEmail(), $this->customer->getPassPlain());
                        if($app_id == $session_app_id && $hd_name['name'] == $hd_session_info['hd_name']&&$hd_name['region'] == $hd_session_info['region'] && !isset($hd_info[0]['name'])){
                          unset($hd_config_info[$app_id]);
                          unset($region_hd[$hd_name['region']][$hd_name['name']]);
                          $this->session->data['app']['hd_deploy'][]= array(
                            'url'=>'hard_disk',
                            'cmd'=>'post',
                            'params'=>array(
                                'username'=>$this->customer->getEmail(),
                                'name'=>$hd_name['name'],
                                'size'=>(int)$hd_session_info['size'],
                                'type'=>(int)$hd_session_info['type'],
                                'region'=>$hd_name['region'],
                                'encryp_type'=>$hd_session_info['encryp_type'],
                                'pin'=>$hd_session_info['pin'],
                            ),
                            'app_id' => $app_id,
                            'hd_name'=>$hd_name['name'],
                            'region' =>$hd_name['region']
                        );
                        }
                    	
                    }
            	 }
            }
			}
           
            //harddisk list that need to patch 
             if(!empty($hd_config_info)){
             	$hd_patch_arr = array();
                foreach($hd_config_info as $app_id => $hd_name){
                	foreach($this->session->data['app']['deploy_config'] as $id => $hd_items){
                		$paramters = $hd_items['params'];
                		$app_type = $paramters['app_type'];
                		$app_name = $paramters['name'];
                		$region = $paramters['region'];
                		
                		if($app_id == $id){
                			$hd_info = $this->backend->get_hd_info($hd_name['region'],$hd_name['name'], $this->customer->getEmail(), $this->customer->getPassPlain());
         	 				if(isset($hd_info[0]['name']) && !isset($hd_patch_arr[$hd_name['region']][$hd_name['name']])){
         	 					unset($region_hd[$hd_name['region']][$hd_name['name']]);
         	 					$hd_patch_arr[$hd_name['region']][$hd_name['name']] = 1;
         	 					$this->session->data['app']['hd_deploy'][]= array(
                                    'url'=>'hard_disk/'.$this->backend->get_hd_url($hd_name['region'],$hd_name['name'],$this->customer->getEmail(),$this->customer->getPassPlain()),
                                    'cmd'=>'patch',
                                    'params'=>array(
                                        'updates'    =>array(
                                            'action'  => 'add',
                                            'path'  => 'resource',
                                            'value'  => '{"resource_type":"'.$app_type.'", "resource_name":"'.$app_name.'"}'
                                        )
                                    ),
                                    'app_id' => $app_id,
                                    'hd_name'=>$hd_name['name'],
                                    'region' =>$hd_name['region']
                 			 );
         	 				}

                		}
                		
                	}
                	
                	
                }
             }
             //fixed loop select  related
            
             if(!empty($region_hd) && count($region_hd) > 0){
             	foreach($region_hd as $region => $value){
 
             		foreach($value as $hd){
             		 foreach($this->session->data['app']['hd']  as $app_id => $hd_info){
             	 		if($region == $hd_info['region'] && $hd['name'] == $hd_info['hd_name']){
             	 			$this->session->data['app']['hd_deploy'][]= array(
                            'url'=>'hard_disk',
                            'cmd'=>'post',
                            'params'=>array(
                                'username'=>$this->customer->getEmail(),
                                'name'=>$hd['name'],
                                'size'=>(int)$hd_info['size'],
                                'type'=>(int)$hd_info['type'],
                                'region'=>$region,
                                'encryp_type'=>$hd_info['encryp_type'],
                                'pin'=>$hd_info['pin'],
                            ),
                            'app_id' => $hd['app_id'],
                            'hd_name'=> $hd['name'],
                            'region' => $region
                        );
                        
             	 			
             	 		}
             	 	
             	 }
             		}
             	}
             	
             	
             }
             
            //end of harddisk
          	//print_r($region_hd);
          	
           
            //print_r($this->session->data['app']['hd_deploy']);
            //循环输出APP所用到的VPC
            foreach($appconfig_info as $vpc_name => $subnet_name){
                //循环输出SESSION 存储的新建VPC
                if(isset($this->session->data['app']['vpc'])){
                    foreach($this->session->data['app']['vpc']  as $vpc_session_info){
                        //如果VPC是新建的
                        if($vpc_name==$vpc_session_info['vpc_name']){
                            unset($appconfig_info[$vpc_name]);
                            $subnet_deploy=array();
                            //循环输出此VPC下用到的subnet
                            foreach($subnet_name as $subnet_name_array){
                                //循环输出 所有新建的subnet
                                foreach($this->session->data['app']['subnet']  as  $subnet_session_info){
                                    //如果subnet是新建的
                                    if($subnet_name_array ==$subnet_session_info['subnet_name'] && $vpc_name==$subnet_session_info['vpc_name']){
                                        $subnet_deploy[]=array(
                                            "vxnet_name"=>(string)$subnet_session_info['subnet_name'],
                                            "vxnet_network"=>"192.168.".$subnet_session_info['network'].".0/24"
                                        );
                                    }
                                }
                            }


                            $this->session->data['app']['eip_deploy'][]= array(
                                'url'=>'public_ip',
                                'cmd'=>'post',
                                'params'=>array(
                                    'username'=>$this->customer->getEmail(),
                                    'name'=>$vpc_session_info['eip_name'],
                                    'bandwidth'=>(int)$vpc_session_info['bandwidth'],
                                    'region'=>$vpc_session_info['region'],
                                )
                            );
                            $this->session->data['app']['vpc_deploy'][]= array(
                                'url'=>'vpc',
                                'cmd'=>'post',
                                'params'=>array(
                                    'username'=>$this->customer->getEmail(),
                                    'name'=>(string)$vpc_name,
                                    'public_ip_name'=>(string)$vpc_session_info['eip_name'],
                                    'region'=>$vpc_session_info['region'],
                                    "vxnet"=>$subnet_deploy
                                )
                            );
                        }
                    }

                }
            }

            if(!empty($appconfig_info)&&isset($this->session->data['app']['subnet'])){
                foreach($appconfig_info as $vpc_name => $subnet_name){


                    $subnet_deploy=array();
                    foreach($subnet_name as $subnet_name_array){
                        //循环输出 所有新建的subnet
                        foreach($this->session->data['app']['subnet']  as  $subnet_session_info){
                            //如果subnet是新建的
                            if($subnet_name_array ==$subnet_session_info['subnet_name'] && $vpc_name==$subnet_session_info['vpc_name']){
                                $this->session->data['app']['vpc_deploy'][]= array(
                                    'url'=>'vpc/'.$this->backend->get_vpc_url($vpc_name,$this->customer->getEmail(),$this->customer->getPassPlain()),
                                    'cmd'=>'patch',
                                    'params'=>array(
                                        'updates'    =>array(
                                            'action'  => 'add',
                                            'path'  => 'vxnet',
                                            'value'  => '{"vxnet_name":"'.$subnet_session_info['subnet_name'].'", "vxnet_network":"192.168.'.$subnet_session_info['network'].'.0/24"}'
                                        )
                                    )
                                );
                            }
                        }
                    }

                }

            }

        }

//        foreach($this->session->data['app']['deploy_config'] as $app_id=>$app_deploy){
//            $this->session->data['app']['deploy_config'][$app_id]['params']['region']=$app_deploy;
//        }


		
        $this->response->setOutput(json_encode($json));

    }
}
